import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
      {
      path: '/',
      name: 'login',
      component: () => import('../views/login/Login.vue')
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/Home.vue'),
      children: [
        {
          path: '',
          component: () => import('../components/home/personalCenter.vue')
        },
        {
          path: 'list',
          component: () => import('../components/home_wzy/list.vue'),
          children:[
            {
              path: 'kpk',
              component: () => import('../components/home_wzy/KaPian.vue')
            }
          ]
        },
        {
          path: 'zf',
          component: () => import('../components/home_wzy/zf.vue')
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  let localStorageSysUserToken = localStorage.getItem("creamDataToken")
  console.log(localStorageSysUserToken)
  console.log(to.path)
  if(to.path === '/' && localStorageSysUserToken) {
    // 如果localStorage中的sysUserToken存在且访问了登录路由
    // 跳转到首页页面
    next({path: '/home'})
  } else if(to.path === '/') {
    // 跳过登录路由/login
    next()
  } else if(!localStorageSysUserToken) {
    // 如果localStorage中的sysUserToken不存在，表示未登录
    // 跳转到登录页面
    next({path: '/'})
  }

  // 其他正常情况，next()继续
  next()
})

export default router
